/******************************************************************************
 * file  : console.c
 * creat : apleilx
 * data  : 2021/01/20
 *******************************************************************************/
#include "pincfg.h"
#include "bsp_api.h"
#include "com_obj.h"
#include "dataproc.h"
#include "framework.h"
#include "simp_txt_analysis.h"
#include "password.h"
#include "iohandle.h"
#include "multi_malloc.h"

/*******************************************************************************
 * @brief  cmd back dat
 *******************************************************************************/
uint32_t cmd_back_dat[20];

/*******************************************************************************
 * @brief  cmd callback interface
 *******************************************************************************/
void cmd_callback(uint16_t cmd_site, const char *txt_src, uint16_t len, int16_t cmd_num);

/*******************************************************************************
 * @brief  cmd index
 *******************************************************************************/
#define CMD_ATTR_ADMIN 1
#define CMD_ATTR_FACTORY (CMD_ATTR_ADMIN + 1)
#define CMD_ATTR_PASSWORD (CMD_ATTR_FACTORY + 1)
#define CMD_ATTR_CMD (CMD_ATTR_PASSWORD + 1)
#define CMD_ATTR_LUT_RD (CMD_ATTR_CMD + 1)
#define CMD_ATTR_LUT_WR (CMD_ATTR_LUT_RD + 1)
#define CMD_ATTR_RS (CMD_ATTR_LUT_WR + 1)
#define CMD_ATTR_DATE_R (CMD_ATTR_RS + 1)
#define CMD_ATTR_DATE_S (CMD_ATTR_DATE_R + 1)
#define CMD_ATTR_DATE (CMD_ATTR_DATE_S + 1)
#define CMD_ATTR_LOG (CMD_ATTR_DATE + 1)
#define CMD_ATTR_CORE_MARK (CMD_ATTR_LOG + 1)
#define CMD_ATTR_READ (CMD_ATTR_CORE_MARK + 1)
#define CMD_ATTR_RST (CMD_ATTR_READ + 1)
#define CMD_ATTR_SDFAT (CMD_ATTR_RST + 1)
#define CMD_ATTR_USBFAT (CMD_ATTR_SDFAT + 1)
#define CMD_ATTR_KEY (CMD_ATTR_USBFAT + 1)
#define CMD_ATTR_USER (CMD_ATTR_KEY + 1)
#define CMD_ATTR_TASK (CMD_ATTR_USER + 1)
#define CMD_ATTR_MUSED (CMD_ATTR_TASK + 1)
#define CMD_ATTR_PWM (CMD_ATTR_MUSED + 1)
#define CMD_ATTR_CAP (CMD_ATTR_PWM + 1)
#define CMD_ATTR_AT (CMD_ATTR_CAP + 1)
#define CMD_ATTR_NOR (CMD_ATTR_AT + 1)
#define CMD_ATTR_SIZE (CMD_ATTR_NOR + 1)

#define CMD_ATTR_END 127

#if CMD_ATTR_SIZE != 25
#error cfg cmd define error "at usb_file_cfg.c"
#endif

/*******************************************************************************
 * @brief  cmd define.
 * bit 15    : not display in info.txt
 * bit 14    : not display in any file
 * bit 8-11  : 0 无需权限， 1-管理权限， 2-工厂权限
 *******************************************************************************/
const txt_cmd_cfg_type cmd_list[] = {
	{0x4000 | CMD_ATTR_ADMIN, "ADMIN.LOGIN", TXT_CMD_TYPE_LONG, cmd_back_dat, 1, cmd_callback},
	{0x4000 | CMD_ATTR_FACTORY, "FACTORY.LOGIN", TXT_CMD_TYPE_LONG, cmd_back_dat, 1, cmd_callback},
	{0x4100 | CMD_ATTR_PASSWORD, "PASSWORD", TXT_CMD_TYPE_STRING, cmd_back_dat, 16, cmd_callback},
	{0x8000 | CMD_ATTR_CMD, "CMD", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback},

	{0x0000 | CMD_ATTR_LUT_RD, "LUTRD", TXT_CMD_TYPE_LONG, cmd_back_dat, 1, cmd_callback},
	{0x0000 | CMD_ATTR_LUT_WR, "LUTWR", TXT_CMD_TYPE_LONG, cmd_back_dat, 1, cmd_callback},
	{0x0000 | CMD_ATTR_RS, "RS", TXT_CMD_TYPE_FLOAT, cmd_back_dat, 5, cmd_callback},
	{0x0000 | CMD_ATTR_DATE_R, "DATE.R", TXT_CMD_TYPE_SHORT, cmd_back_dat, 3, cmd_callback},
	{0x0000 | CMD_ATTR_DATE_S, "DATE.S", TXT_CMD_TYPE_SHORT, cmd_back_dat, 3, cmd_callback},
	{0x0000 | CMD_ATTR_DATE, "DATE", TXT_CMD_TYPE_SHORT, cmd_back_dat, 6, cmd_callback},
	{0x0000 | CMD_ATTR_LOG, "LOG", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback},
	{0x0000 | CMD_ATTR_CORE_MARK, "COREMARK", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback},

	{0x8100 | CMD_ATTR_RST, "CLR", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback},
	{0x8100 | CMD_ATTR_READ, "READ", TXT_CMD_TYPE_STRING, cmd_back_dat, 50, cmd_callback},
	{0x8100 | CMD_ATTR_SDFAT, "SDFAT", TXT_CMD_TYPE_STRING, cmd_back_dat, 24, cmd_callback},
	{0x8100 | CMD_ATTR_USBFAT, "USBFAT", TXT_CMD_TYPE_STRING, cmd_back_dat, 24, cmd_callback},
	{0x8100 | CMD_ATTR_KEY, "KEY[]", TXT_CMD_TYPE_CHAR, cmd_back_dat, 1, cmd_callback},
	{0x8100 | CMD_ATTR_USER, "USER", TXT_CMD_TYPE_SHORT, cmd_back_dat, 1, cmd_callback},
	{0x8100 | CMD_ATTR_TASK, "OS-TASK", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback},
	{0x8100 | CMD_ATTR_MUSED, "MUSED", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback},
	{0x8100 | CMD_ATTR_PWM, "PWM", TXT_CMD_TYPE_LONG, cmd_back_dat, 2, cmd_callback},
	{0x8100 | CMD_ATTR_CAP, "CAP", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback},
	{0x8100 | CMD_ATTR_AT, "AT", TXT_CMD_TYPE_STRING, cmd_back_dat, 60, cmd_callback},
	{0x0000 | CMD_ATTR_NOR, "NOR", TXT_CMD_TYPE_LONG, cmd_back_dat, 4, cmd_callback},
	{CMD_ATTR_END, "END", TXT_CMD_TYPE_END, cmd_back_dat, 1, cmd_callback},
};

/*******************************************************************************
 * @brief  cmd_callback.
 * \param[in] cmd_site 命令位置
 * \param[in] txt_src 文本原文
 * \param[in] len 数据长度
 * \param[in] cmd_num 数组模式cmd索引
 * \retval: none
 *******************************************************************************/
void cmd_callback(uint16_t cmd_site, const char *txt_src, uint16_t len, int16_t cmd_num)
{
	char *i8p;
	int16_t *i16p;
	int32_t *i32p;

	if (cmd_site > sizeof(cmd_list) / sizeof(txt_cmd_cfg_type))
		return;

	i8p = cmd_list[cmd_site].des;
	i16p = cmd_list[cmd_site].des;
	i32p = cmd_list[cmd_site].des;

	(void)i8p;
	(void)i16p;
	(void)i32p;

	switch (cmd_list[cmd_site].attr & 0xFF)
	{
	case CMD_ATTR_ADMIN:
	{
		dbg_puts("CMD_ADMIN\n");
		break;
	}
	case CMD_ATTR_FACTORY:
	{
		dbg_puts("CMD_FACTORY\n");
		break;
	}
	case CMD_ATTR_PASSWORD:
	{
		char pwd[12], pwsrc[32];
		int src_len = strlen(i8p);

		dbg_print("PASSWORD SRC LEN = %d \n", src_len);

		strcpy(pwsrc, i8p);
		pwsrc[src_len] = 0;
		string6_passward_creat(pwsrc, pwd, 1);
		dbg_print("PASSWORD 6 string = %s \n", pwd);

		strcpy(pwsrc, i8p);
		pwsrc[src_len] = 0;
		string8_passward_creat(pwsrc, pwd, 1);
		dbg_print("PASSWORD 8 string = %s \n", pwd);

		strcpy(pwsrc, i8p);
		pwsrc[src_len] = 0;
		string10_passward_creat(pwsrc, pwd, 1);
		dbg_print("PASSWORD 10 string = %s \n", pwd);

		break;
	}
	case CMD_ATTR_CMD:
	{
		break;
	}
	case CMD_ATTR_LUT_RD:
	{
		// lut_read_req(*i32p);
		break;
	}
	case CMD_ATTR_LUT_WR:
	{
		// lut_write_req(*i32p);
		break;
	}
	case CMD_ATTR_RS:
	{
		// i32p[0] = 0xD0000000;
		// i32p[1] = 0x70000000;
		dbg_print("RS NUM %d:", len);
		dbg_u32_print("F: DAT :", i8p, 5, 6);
		break;
	}
	case CMD_ATTR_DATE_R:
	{
		break;
	}
	case CMD_ATTR_DATE_S:
	{
		break;
	}
	case CMD_ATTR_DATE:
	{
		break;
	}
	case CMD_ATTR_LOG:
	{
		break;
	}
	case CMD_ATTR_CORE_MARK:
	{
		break;
	}
	case CMD_ATTR_RST:
	{
		break;
	}
	case CMD_ATTR_READ:
	{
		break;
	}
	case CMD_ATTR_SDFAT:
	{
		break;
	}
	case CMD_ATTR_USBFAT:
	{
		break;
	}
	case CMD_ATTR_KEY:
	{
		break;
	}
	case CMD_ATTR_USER:
	{
		break;
	}
	case CMD_ATTR_MUSED:
	{
		break;
	}
	case CMD_ATTR_PWM:
	{
		break;
	}
	case CMD_ATTR_CAP:
	{
		break;
	}
	case CMD_ATTR_AT:
	{
		dbg_puts((char *)i8p);
		dbg_puts("\n");
		break;
	}
	case CMD_ATTR_NOR:
	{
		break;
	}
	default:
	{
		break;
	}
	}
}
